﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using libtcod;

namespace CSRoguelike
{
    class Room
    {
        public int x, y, w, h;

        public Room(int x, int y, int width, int height)
        {
            this.x = x;
            this.y = y;
            this.w = width;
            this.h = height;
        }
    }

    class Map
    {
        private const int MAX_ROOM_SIZE = 12;
        private const int MIN_ROOM_SIZE = 6;
        private const int MIN_ROOM_COUNT = 6;
        private const int MAX_ROOM_COUNT = 20;
        private TCODColor lightWall = TCODColor.darkBlue;
        private TCODColor lightGround = TCODColor.black;

        public Tile[,] _Map;
        private List<Room> rooms;
        private int width, height;

        public Map(int w, int h, ref Actor player)
        {
            this.width = w;
            this.height = h;
            _Map = new Tile[w,h];

            for (int x = 0; x < w; x++)
            {
                for (int y = 0; y < h; y++)
                {
                    _Map[x, y] = new Tile(true, true, false);
                }
            }

            rooms = new List<Room>();
            GenerateMap(ref player);
        }

        public void Render(TCODConsole con)
        {
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    if(_Map[x,y].BlockedSight)
                    {
                        con.setCharBackground(x, y, lightWall);
                    } 
                    else 
                    {
                        con.setCharBackground(x, y, lightGround);
                    }
                }
            }
        }

        public void GenerateMap(ref Actor player)
        {
            TCODRandom rng = TCODRandom.getInstance();

            // Make the rooms
            int RoomCount = rng.getInt(MIN_ROOM_COUNT, MAX_ROOM_COUNT);
            for (int i = 0; i < RoomCount; i++)
            {
                int w = rng.getInt(MIN_ROOM_SIZE, MAX_ROOM_SIZE);
                int h = rng.getInt(MIN_ROOM_SIZE, MAX_ROOM_SIZE);

                int x = rng.getInt(0, width - w);
                int y = rng.getInt(0, height - h);

                if (i == 0)
                {
                    player.SetLocation(x + (w / 2), y + (h / 2));
                }
                rooms.Add(new Room(x, y, w, h));
                Dig(x, x + w, y, y + h);
            }

            // Connect the rooms.
        }

        private void Dig(int x1, int x2, int y1, int y2)
        {
            for (int x = x1; x < x2; x++)
            {
                for (int y = y1; y < y2; y++)
                {
                    _Map[x, y].Blocked = false;
                    _Map[x, y].BlockedSight = false;
                    _Map[x, y].IsPassable = true;
                }
            }
        }

        public bool IsValid(int x, int y)
        {
            if (x < 0 || x >= width || y < 0 || y >= height) // Check Map constraints.
            {
                return false;
            }

            if (!_Map[x, y].IsPassable)
                return false;
            return true;
        }
    }
}
